Event = require 'KbePlugins/events'

Vector2Ex = require "extend/Vector2Ex"
Vector3Ex = require "extend/Vector3Ex"
Vector4Ex = require "extend/Vector4Ex"
QuaternionEx = require "extend/QuaternionEx"

require "Common/UtilFunction"

require "Common/ClassUtil"
require "Common/SerializeClassUtil"

require "extend/Vector2Pool"
require "extend/Vector3Pool"
require "extend/Vector4Pool"
require "extend/QuaternionPool"
-- require "extend/TablePool"
require "extend/LuaArrayPool"
require "extend/LuaDictPool"
require "extend/LuaListPool"
require "extend/LuaObjectPool"
require "extend/TableObjectPool"

require "extend/BaseTableObject"


require "Logic/SettingEx"
require "Logic/LuaTextKey"
require "Logic/TipManager"
require "ScriptableObject/ScriptableObjectManager"
require "Logic/LuaFileLog"
require "Logic/JsonConfig"
json = require "cjson"

require "Logic/BaseConfig"
Config = BaseConfig.New("config")


GameObject = UnityEngine.GameObject

--场景切换通知
--function OnLevelWasLoaded(level)
--	Time.timeSinceLevelLoad = 0
--end



--主入口函数。从这里开始lua逻辑
function Main()
	math.randomseed(os.time())

	if SettingEx.bool.isEnableGuiLog then
		SwitchConfig.isEnableGuiLog = SettingEx.bool.isEnableGuiLog
		Login.UpdateGuiLog()
	end
	
	MyEventObject.instance:RegisterOnGUI(OnGUI)

	
	-- -- 请求权限
	-- AndroidHelper.RequestAndroidPermission("android.permission.READ_PHONE_STATE", nil)
end



function OnApplicationQuit()
	if MyEventObject.instance then
		MyEventObject.instance:UnregisterOnGUI(OnGUI)
	end
end

local log = ""
local logTime = 0
local updateTime = 1



function GetOnlineTime()
	local time = math.modf(Time.realtimeSinceStartup)
	local hour = math.modf(time / 60 / 60)
	local min = math.modf(time / 60 - hour * 60)
	local second = math.modf(time - hour * 60 * 60 - min * 60)

	return string.format("%02d:%02d:%02d", hour, min, second)
end

function OnGUI()
	
	if logTime == 0 or Time.time - logTime >= updateTime then
		local mem = collectgarbage("count")
		logTime = Time.time
		log = ""
		log = log.."ct:"..tostring(VersionEx.compile_type).." v:"..VersionEx.version.." vn:"..tostring(VersionEx.version_number).." bn:"..tostring(VersionEx.build_number).." abv:"..tostring(BundleVersionManager.instance:GetVersion()).." mav:"..tostring(BundleVersionManager.instance:GetMinAppVersion()).." erv:"..tostring(ExternalResourceManager.GetVersion())
		
		-- log = log.."\n".."kv:"..tostring(KBEngineLua.clientVersion).." ".."sip:"..tostring(KBEngineLua.ip)..":"..tostring(KBEngineLua.port).."|"..tostring(KBEngineLua.baseappIP)..":"..tostring(KBEngineLua.baseappPort)
		-- log = log.."\nns:"..KBEngineLua.networkState().." rs:"..string.format("%0.1f", KBEngineLua.GetReceiveSpeed() / 1024).."k/s ss:"..string.format("%0.1f", KBEngineLua.GetSendSpeed() / 1024).."k/s"
		log = log.."\n"..tostring("lv:"..tostring(_VERSION))
		if jit then
			log = log.." jv:"..jit.version_num.." js:"..tostring(jit.status()).." jo:"..tostring(jit.os).." ja:"..tostring(jit.arch)
		end

		log = log.."\nuv:"..UnityEngine.Application.unityVersion.."  graphicDevice:"..tostring(UnityEngine.SystemInfo.graphicsDeviceType)
		log = log.."\nip:"..Login.GetIP().."  time:"..GetOnlineTime()

		

		-- local arrCount, arrElemCount = KBEngine.MemoryStreamPool.GetByteArrayPoolSize(nil)
		-- local cSharpMem = System.GC.GetTotalMemory(false)
		-- log = log.."\nlua_mem:"..string.format("%6.2f", mem / 2014).."m".."  c#_mem:"..tostring(cSharpMem / 1024 / 1024).."m".."  msp_size:"..tostring(KBEngine.MemoryStreamPool.GetSize()).."  bap_size:"..tostring(arrCount)..","..tostring(arrElemCount)
		-- local player = KBEngineLua.player()
		-- local playerMemInc = nil
		-- if player then
		-- 	if player.gameEntity then
		-- 		playerMemInc = player.gameEntity.memInc
		-- 	end
		-- end
		log = log.."\nu:"..UpdateBeat:Count().."|"..tostring(UpdateBeat.memInc).."  lu:"..LateUpdateBeat:Count().."|"..tostring(LateUpdateBeat.memInc).."  fu:"..FixedUpdateBeat:Count().."|"..tostring(FixedUpdateBeat.memInc).."  cu:"..CoUpdateBeat:Count().."|"..tostring(CoUpdateBeat.memInc)
		log = log.."\nss:"..UnityEngine.Screen.width.." x "..UnityEngine.Screen.height
		-- .."  kbe:"..tostring(KBEngineLua.memInc).."  player:"..tostring(playerMemInc)
		
		-- .." bt:"..tostring(VersionEx.build_time)
	end

	GuiUtil.Label(log, 20, bit.bor(LabelPos.R, LabelPos.T), 0, 0)
end
